package com.example.demo.clid;

import com.example.demo.dao1.ClassJpaDao;
import com.example.demo.dao1.StuJpaDao;
import com.example.demo.entity.Classa;
import com.example.demo.entity.Stucu;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class ThymelefController {
    @Resource
    ClassJpaDao classJpaDao;
    @Resource
    StuJpaDao stuJpaDao;

    public ThymelefController() {
    }

    @GetMapping({"/toLogninPage"})
    public String deleteJpa(Model model, int id) {
        Classa classa = (Classa) this.classJpaDao.findById(id).get();
        model.addAttribute("aaa", classa.getName());
        model.addAttribute("bbb", classa);
        return "login";
    }

    @GetMapping({"/toLogninPage2"})
    public String deleteJpa3(Model uu) {
        List<Stucu> stucu = this.stuJpaDao.findAll();
        uu.addAttribute("cc", stucu);
        return "stu";
    }

    @GetMapping({"/toLogninPage5"})
    public String deleteJpa(Model tt) {
        List<Classa> classa = this.classJpaDao.findAll();
        tt.addAttribute("gg", classa);
        return "login";
    }

    @GetMapping("/toLoginPage2")//访问路径
    public String toLoginPage1(Model ab,
                               @RequestParam(defaultValue = "") String name,
                               @RequestParam(defaultValue = "0") int age) {
        List<Stucu> list;
        if(age ==0){
            list = stuJpaDao.findByNameContaining(name);//根据学生姓名查询
        }else {
            list = stuJpaDao.findByNameContainingAndAge(name,age);//根据学生姓名和年龄查询
        }
        ab.addAttribute("aaa",list);//把查出来的学生列表放到索引aaa里
        ab.addAttribute("bbb",name);//把查出来的学生列表放到索引aaa里
        return "stu";//返回一个网页的名称，同时保证templates目录下要有对应的名称.html
    }

    @GetMapping({"/toLogninPage51"})
    public String deleteJpa(Model tt, String name, String dep) {
        List<Classa> classa = this.classJpaDao.findByNameContainingOrDep(name, dep);
        tt.addAttribute("gg", classa);
        tt.addAttribute("bbb", name);
        return "login";
    }

    @GetMapping({"/vue"})
    public String vue() {
        return "vue";
    }
}
